package com.example.pkmedical.Activity.rollovershock;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextClock;
import android.widget.TextView;

import com.example.pkmedical.Base.BaseActivity;
import com.example.pkmedical.R;
import com.example.pkmedical.Utils.SPUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.example.pkmedical.Utils.CommandUtil.SHOCK_POSITION_TYPE;


public class PositionChangeActivity extends BaseActivity {

    @BindView(R.id.main_house)
    ImageView mainHouse;
    @BindView(R.id.main_house_text)
    TextView mainHouseText;
    @BindView(R.id.main_date)
    TextClock mainDate;
    @BindView(R.id.id_poschange_zuoce_img)
    ImageView idPoschangeZuoceImg;
    @BindView(R.id.pos_change_leftdown_layout)
    RelativeLayout posChangeLeftdownLayout;
    @BindView(R.id.id_poschange_pingwo_img)
    ImageView idPoschangePingwoImg;
    @BindView(R.id.pos_change_flat_layout)
    RelativeLayout posChangeFlatLayout;
    @BindView(R.id.id_poschange_youce_img)
    ImageView idPoschangeYouceImg;
    @BindView(R.id.pos_change_rightdown_layout)
    RelativeLayout posChangeRightdownLayout;
    @BindView(R.id.id_poschange_cefan_img)
    ImageView idPoschangeCefanImg;
    @BindView(R.id.pos_change_rollover_layout)
    RelativeLayout posChangeRolloverLayout;
    @BindView(R.id.pos_change_setting_layout_text)
    TextView posChangeSettingLayoutText;
    @BindView(R.id.pos_change_setting_layout)
    RelativeLayout posChangeSettingLayout;
    @BindView(R.id.positionchange_goback_bt)
    Button positionchangeGobackBt;
    @BindView(R.id.pos_change_bfb_layout)
    LinearLayout posChangeBfbLayout;
    @BindView(R.id.pos_change_roll_layout)
    LinearLayout posChangeRollLayout;
    @BindView(R.id.pos_change_high)
    RelativeLayout posChangeHigh;
    @BindView(R.id.pos_change_secondary)
    RelativeLayout posChangeSecondary;
    @BindView(R.id.pos_change_low)
    RelativeLayout posChangeLow;
    @BindView(R.id.pos_change_custonized)
    RelativeLayout posChangeCustonized;
    @BindView(R.id.left_one_text)
    TextView leftOneText;
    @BindView(R.id.left_two_text)
    TextView leftTwoText;
    @BindView(R.id.left_three_text)
    TextView leftThreeText;
    @BindView(R.id.left_four_text)
    TextView leftFourText;
    @BindView(R.id.right_one_text)
    TextView rightOneText;
    @BindView(R.id.right_two_text)
    TextView rightTwoText;
    @BindView(R.id.right_three_text)
    TextView rightThreeText;
    @BindView(R.id.right_four_text)
    TextView rightFourText;
    private PopupWindow bfbPop;
    private int xzType = 1;//左还是右的数据1：左，2：右


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_activity_positionchange);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        mainHouse.setImageResource(R.drawable.img_title_fbzl);
        mainHouseText.setText(R.string.change_position);
//        one_on();
//        rightOne_on();
    }

    @Override
    public void initView() {
        switch (SHOCK_POSITION_TYPE) {
            case 0:
                posChangeSettingLayout.setVisibility(View.GONE);
                two_on();
                break;
            case 1:
                one_on();
                break;
            case 2:
                three_on();
                break;
            case 3:
                four_on();
                rightOne_on();
                break;
            case 4:
                four_on();
                rightTwo_on();
                break;
            case 5:
                four_on();
                rightThree_on();
                break;
            case 6:
                four_on();
                rightFour_on();
                break;
        }
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void showDialog(String... msg) {

    }

    @OnClick({R.id.pos_change_high, R.id.pos_change_secondary, R.id.pos_change_low, R.id.pos_change_custonized, R.id.pos_change_leftdown_layout, R.id.pos_change_flat_layout, R.id.pos_change_rightdown_layout, R.id.pos_change_rollover_layout, R.id.pos_change_setting_layout, R.id.positionchange_goback_bt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.pos_change_leftdown_layout://点击左侧的左翻
                SHOCK_POSITION_TYPE = 1;
                xzType = 1;
                posChangeSettingLayoutText.setText(SPUtils.getInstance().getInt("rollover_left_bfb") * 10 + "%");
                one_on();
                show(1);
                break;
            case R.id.pos_change_flat_layout:
                SHOCK_POSITION_TYPE = 0;
                two_on();
                show(1);
                break;
            case R.id.pos_change_rightdown_layout:
                SHOCK_POSITION_TYPE = 2;
                xzType = 2;
                posChangeSettingLayoutText.setText(SPUtils.getInstance().getInt("rollover_right_bfb") * 10 + "%");
                three_on();
                show(1);
                break;
            case R.id.pos_change_rollover_layout:
                SHOCK_POSITION_TYPE = 3;
                four_on();
                break;
            case R.id.pos_change_setting_layout:
                initNumWindow(posChangeSettingLayoutText);
                break;
            case R.id.positionchange_goback_bt:
                finish();
                overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
                break;
            case R.id.pos_change_high:
                SHOCK_POSITION_TYPE = 3;
                rightOne_on();
                break;
            case R.id.pos_change_secondary:
                SHOCK_POSITION_TYPE = 4;
                rightTwo_on();
                break;
            case R.id.pos_change_low:
                SHOCK_POSITION_TYPE = 5;
                rightThree_on();
                break;
            case R.id.pos_change_custonized:
                SHOCK_POSITION_TYPE = 6;
                rightFour_on();
                break;
        }
    }

    private void rightOne_on() {
        posChangeHigh.setBackgroundResource(R.drawable.qdms_item_back_on);
        rightOneText.setTextColor(getResources().getColor(R.color.green_back_text));
        posChangeSecondary.setBackgroundResource(R.drawable.changgui_item_back_off);
        rightTwoText.setTextColor(getResources().getColor(R.color.white));
        posChangeLow.setBackgroundResource(R.drawable.changgui_item_back_off);
        rightThreeText.setTextColor(getResources().getColor(R.color.white));
        posChangeCustonized.setBackgroundResource(R.drawable.changgui_item_back_off);
        rightFourText.setTextColor(getResources().getColor(R.color.white));


    }

    private void rightTwo_on() {
        posChangeHigh.setBackgroundResource(R.drawable.changgui_item_back_off);
        rightOneText.setTextColor(getResources().getColor(R.color.white));

        posChangeSecondary.setBackgroundResource(R.drawable.qdms_item_back_on);
        rightTwoText.setTextColor(getResources().getColor(R.color.green_back_text));

        posChangeLow.setBackgroundResource(R.drawable.changgui_item_back_off);
        rightThreeText.setTextColor(getResources().getColor(R.color.white));

        posChangeCustonized.setBackgroundResource(R.drawable.changgui_item_back_off);
        rightFourText.setTextColor(getResources().getColor(R.color.white));
    }

    private void rightThree_on() {
        posChangeHigh.setBackgroundResource(R.drawable.changgui_item_back_off);
        rightOneText.setTextColor(getResources().getColor(R.color.white));

        posChangeSecondary.setBackgroundResource(R.drawable.changgui_item_back_off);
        rightTwoText.setTextColor(getResources().getColor(R.color.white));

        posChangeLow.setBackgroundResource(R.drawable.qdms_item_back_on);
        rightThreeText.setTextColor(getResources().getColor(R.color.green_back_text));

        posChangeCustonized.setBackgroundResource(R.drawable.changgui_item_back_off);
        rightFourText.setTextColor(getResources().getColor(R.color.white));
    }

    private void rightFour_on() {
        posChangeHigh.setBackgroundResource(R.drawable.changgui_item_back_off);
        rightOneText.setTextColor(getResources().getColor(R.color.white));

        posChangeSecondary.setBackgroundResource(R.drawable.changgui_item_back_off);
        rightTwoText.setTextColor(getResources().getColor(R.color.white));

        posChangeLow.setBackgroundResource(R.drawable.changgui_item_back_off);
        rightThreeText.setTextColor(getResources().getColor(R.color.white));

        posChangeCustonized.setBackgroundResource(R.drawable.qdms_item_back_on);
        rightFourText.setTextColor(getResources().getColor(R.color.green_back_text));
    }

    private int bfbNow;

    /**
     * %选择
     *
     * @param textView
     */
    private void initNumWindow(final TextView textView) {
        View view = LayoutInflater.from(this).inflate(R.layout.item_time, null);
        final TextView tv_it_time = view.findViewById(R.id.tv_it_time);
        ImageView iv_it_left = view.findViewById(R.id.iv_it_left);
        ImageView iv_it_right = view.findViewById(R.id.iv_it_right);
        TextView tv_it_mtime = view.findViewById(R.id.tv_it_mtime);
        final SeekBar sb_it = view.findViewById(R.id.sb_it);
        sb_it.setMax(10);
        tv_it_mtime.setText("100");
        String btStr = textView.getText().toString();
        bfbNow = Integer.parseInt(btStr.substring(0, btStr.indexOf("%")));
        tv_it_time.setText(bfbNow + "");
        sb_it.setProgress(bfbNow / 10);
        iv_it_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (bfbNow > 10) {
                    bfbNow = bfbNow - 10;
                    sb_it.setProgress(bfbNow / 10);
                }
                switch (xzType) {
                    case 1:
                        SPUtils.getInstance().put("rollover_left_bfb", bfbNow / 10);
                        break;
                    case 2:
                        SPUtils.getInstance().put("rollover_right_bfb", bfbNow / 10);
                        break;
                }
            }
        });
        iv_it_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (bfbNow < 100) {
                    bfbNow = bfbNow + 10;
                    sb_it.setProgress(bfbNow / 10);
                }
                switch (xzType) {
                    case 1:
                        SPUtils.getInstance().put("rollover_left_bfb", bfbNow / 10);
                        break;
                    case 2:
                        SPUtils.getInstance().put("rollover_right_bfb", bfbNow / 10);
                        break;
                }
            }
        });
        sb_it.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                if (i > 1) {
                    bfbNow = i * 10;
                    textView.setText(bfbNow + "%");
                    tv_it_time.setText(bfbNow + "");
                } else {
                    seekBar.setProgress(1);
                    bfbNow = 10;
                    textView.setText(bfbNow + "%");
                    tv_it_time.setText(bfbNow + "");
                }
                switch (xzType) {
                    case 1:
                        SPUtils.getInstance().put("rollover_left_bfb", bfbNow / 10);
                        break;
                    case 2:
                        SPUtils.getInstance().put("rollover_right_bfb", bfbNow / 10);
                        break;
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        bfbPop = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        bfbPop.setFocusable(true);
        bfbPop.setOutsideTouchable(true);
        bfbPop.setBackgroundDrawable(new ColorDrawable());
        int xoff = (bfbPop.getContentView().getMeasuredWidth() - textView.getWidth()) / 2;
        bfbPop.showAsDropDown(textView, -xoff, 0);
    }

    /**
     * 布局显示1：百分比，2：低中高
     *
     * @param i
     */
    private void show(int i) {
        if (1 == i) {
            posChangeBfbLayout.setVisibility(View.VISIBLE);
            posChangeRollLayout.setVisibility(View.GONE);
        } else {
            posChangeBfbLayout.setVisibility(View.GONE);
            posChangeRollLayout.setVisibility(View.VISIBLE);
        }
    }

    private void one_on() {
        posChangeSettingLayoutText.setText(SPUtils.getInstance().getInt("rollover_left_bfb") * 10 + "%");
        posChangeSettingLayout.setVisibility(View.VISIBLE);
        posChangeLeftdownLayout.setBackgroundResource(R.drawable.qdms_item_back_on);
        leftOneText.setTextColor(getResources().getColor(R.color.green_back_text));
        idPoschangeZuoceImg.setImageResource(R.drawable.img_zuoce_on);

        posChangeFlatLayout.setBackgroundResource(R.drawable.changgui_item_back_off);
        leftTwoText.setTextColor(getResources().getColor(R.color.white));
        idPoschangePingwoImg.setImageResource(R.drawable.img_pingwo_off);

        posChangeRightdownLayout.setBackgroundResource(R.drawable.changgui_item_back_off);
        leftThreeText.setTextColor(getResources().getColor(R.color.white));
        idPoschangeYouceImg.setImageResource(R.drawable.img_youce_off);

        posChangeRolloverLayout.setBackgroundResource(R.drawable.changgui_item_back_off);
        leftFourText.setTextColor(getResources().getColor(R.color.white));
        idPoschangeCefanImg.setImageResource(R.drawable.img_cefan_off);
    }

    private void two_on() {
        posChangeSettingLayout.setVisibility(View.GONE);
        posChangeLeftdownLayout.setBackgroundResource(R.drawable.changgui_item_back_off);
        leftOneText.setTextColor(getResources().getColor(R.color.white));
        idPoschangeZuoceImg.setImageResource(R.drawable.img_zuoce_off);

        posChangeFlatLayout.setBackgroundResource(R.drawable.qdms_item_back_on);
        leftTwoText.setTextColor(getResources().getColor(R.color.green_back_text));
        idPoschangePingwoImg.setImageResource(R.drawable.img_pingwo_on);

        posChangeRightdownLayout.setBackgroundResource(R.drawable.changgui_item_back_off);
        leftThreeText.setTextColor(getResources().getColor(R.color.white));
        idPoschangeYouceImg.setImageResource(R.drawable.img_youce_off);

        posChangeRolloverLayout.setBackgroundResource(R.drawable.changgui_item_back_off);
        idPoschangeCefanImg.setImageResource(R.drawable.img_cefan_off);
        leftFourText.setTextColor(getResources().getColor(R.color.white));
    }

    private void three_on() {
        posChangeSettingLayoutText.setText(SPUtils.getInstance().getInt("rollover_right_bfb") * 10 + "%");
        posChangeSettingLayout.setVisibility(View.VISIBLE);
        posChangeLeftdownLayout.setBackgroundResource(R.drawable.changgui_item_back_off);
        leftOneText.setTextColor(getResources().getColor(R.color.white));
        idPoschangeZuoceImg.setImageResource(R.drawable.img_zuoce_off);

        posChangeFlatLayout.setBackgroundResource(R.drawable.changgui_item_back_off);
        leftTwoText.setTextColor(getResources().getColor(R.color.white));
        idPoschangePingwoImg.setImageResource(R.drawable.img_pingwo_off);

        posChangeRightdownLayout.setBackgroundResource(R.drawable.qdms_item_back_on);
        leftThreeText.setTextColor(getResources().getColor(R.color.green_back_text));
        idPoschangeYouceImg.setImageResource(R.drawable.img_youce_on);

        posChangeRolloverLayout.setBackgroundResource(R.drawable.changgui_item_back_off);
        idPoschangeCefanImg.setImageResource(R.drawable.img_cefan_off);
        leftFourText.setTextColor(getResources().getColor(R.color.white));
    }

    private void four_on() {
        show(2);
        rightOne_on();
        posChangeLeftdownLayout.setBackgroundResource(R.drawable.changgui_item_back_off);
        leftOneText.setTextColor(getResources().getColor(R.color.white));
        idPoschangeZuoceImg.setImageResource(R.drawable.img_zuoce_off);

        posChangeFlatLayout.setBackgroundResource(R.drawable.changgui_item_back_off);
        leftTwoText.setTextColor(getResources().getColor(R.color.white));
        idPoschangePingwoImg.setImageResource(R.drawable.img_pingwo_off);

        posChangeRightdownLayout.setBackgroundResource(R.drawable.changgui_item_back_off);
        leftThreeText.setTextColor(getResources().getColor(R.color.white));
        idPoschangeYouceImg.setImageResource(R.drawable.img_youce_off);

        posChangeRolloverLayout.setBackgroundResource(R.drawable.qdms_item_back_on);
        idPoschangeCefanImg.setImageResource(R.drawable.img_cefan_on);
        leftFourText.setTextColor(getResources().getColor(R.color.green_back_text));
    }
}
